const webpack = require('webpack');
const path = require('path');

const babelLoaderConfig = {
  presets: ['es2015', 'stage-0', 'react'],
  plugins: [['import', {libraryName: 'antd','style': true}]],  // antd模块化加载, https://github.com/ant-design/babel-plugin-antd
};

module.exports = {
  // entry: [
  //   // 可能需要polyfill
  //   './src/index.js',
  // ],
  entry: {
    "common": [
      "react",
      "react-dom",
      "react-router",
      'antd/lib/button',
      'antd/lib/table',
      'antd/lib/modal',
      'antd/lib/message',
      'antd/lib/form',
      'antd/lib/menu',
      'antd/lib/row',
      'antd/lib/col',
      'antd/lib/tooltip',
      'antd/lib/radio',
      'antd/lib/date-picker',
      'antd/lib/time-picker',
      'antd/lib/input',
      'antd/lib/input-number',
      'antd/lib/notification',
      'antd/lib/pagination',
      'antd/lib/select',
      'antd/lib/tag',
      // "antd",
      // "jquery"
      // 'echarts',
      // 'g2',
    ],
    "bundle":[
      // 'webpack-dev-server/client?http://0.0.0.0:3000', // WebpackDevServer host and port
      // 'webpack/hot/only-dev-server', // "only" prevents reload on syntax errors
      'babel-polyfill',  // 可以使用完整的ES6特性, 大概增加100KB
      './src/index.js',  // 编译的入口
    ],
  },
  output: {
    path: __dirname + '/dist',
    filename: '[name].min.js',
    chunkFilename: '[name].[chunkhash:5].js',
    publicPath: 'http://docker.heatedloan.com:3000/',
    // publicPath: 'http://mycdn.com/', // require时用来生成图片的地址
  },

  resolve: {
    modulesDirectories: ['node_modules', './src'],
    extensions: ['', '.js', '.jsx'],
    alias: {
      antdcss: 'antd/dist/antd.min.css',  // import时的别名
      components: path.resolve(__dirname, 'src/components'),
      app: path.resolve(__dirname, 'src/App'),
      'react/lib/ReactMount': 'react-dom/lib/ReactMount',
      'react/lib/ReactDom': 'react-dom/lib/ReactDom',
    },
  },

  module: {
    loaders: [
      {
        test: /\.jsx?$/,
        // 删除一些debug语句
        loaders: ['babel-loader?' + JSON.stringify(babelLoaderConfig), 'strip-loader?strip[]=logger.debug,strip[]=console.log,strip[]=console.debug'],
        exclude: /node_modules/,
      }, {
        test: /\.css$/,
        loader: 'style!css',
      }, {
        test: /\.less$/,
        loader: 'style!css!less',
      }, {
        test: /\.(png|jpg|svg)$/,
        loader: 'url?limit=25000',
      },
    ],
  },

  plugins: [
    // 代码压缩
    new webpack.optimize.UglifyJsPlugin({
      minimize: true,
      compress: {
        warnings: false,
        drop_console: true,
      },
    }),

    // 抽离公共部分
    // webpack.optimize.CommonsChunkPlugin

    new webpack.optimize.DedupePlugin(),
    // 比对id的使用频率和分布来得出最短的id分配给使用频率高的模块
    new webpack.optimize.OccurenceOrderPlugin(),
    // 这个插件会把按需加载都打包进一个
    // new webpack.optimize.AggressiveMergingPlugin(),
    // 抽离公共部分
    new webpack.optimize.CommonsChunkPlugin({
      name: ['common'],//页面上使用的时候common
      // chunks:['antd','common'],
      //必须最先加载
      filename:"common.min.js",//忽略则以name为输出文件的名字，
          // 否则以此为输出文件名字
      // minChunks: 2
    }),
    // 允许错误不打断程序
    new webpack.NoErrorsPlugin(),

    new webpack.DefinePlugin({
      'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
      __DEV__: JSON.stringify(JSON.parse(process.env.NODE_ENV === 'production' ? 'false' : 'true')),
    }),
  ],
};
